//
//  CustomAnnotationView.m
//  testMap
//
//  Created by 林振宇 on 15/4/22.
//  Copyright (c) 2015年 www.lanou3g.com. All rights reserved.
//

#define kCalloutWidth       200.0
#define kCalloutHeight      70.0


#import "CustomAnnotationView.h"



@interface CustomAnnotationView ()

@property (nonatomic, retain) CustomCalloutView *calloutView;

@end

@implementation CustomAnnotationView
@synthesize calloutView  = _calloutView;
#pragma mark - 选中气泡的方法,重写父类

// 懒加载
//- (CustomCalloutView *)calloutView
//{
//    if (_calloutView == nil) {
//        _calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
//    }
//    
//    return _calloutView;
//}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  
    
    //当前状态 和 用户选择的状态一样就 return
    if (self.selected == selected)
    {
        return;
    }
    //如果是选中状态
    if(selected)
    {
        
     
       //弹出气泡
        
        //初始化
        if (self.calloutView == nil)
        {
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth+50, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
            
        }
        self.calloutView.image = [UIImage imageNamed:@"building.png"];
      // NSLog(@"qqqqqq%@",self.annotation.subtitle);
        self.calloutView.title = [self.annotation title];
        self.calloutView.subtitle = [self.annotation subtitle];
        self.calloutView.details = [NSString stringWithFormat:@"目的地 : %@\n详细地址 : %@",[self.annotation title],[self.annotation subtitle]];
        [self addSubview:self.calloutView];
    }
    //如果是费选中状态
    else
    {
        //移除气泡
        [self.calloutView removeFromSuperview];
    }
    [super setSelected:selected animated:animated];
}
// 重写此函数，用以实现点击calloutView判断为点击该annotationView , 点击气泡不消失
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
